﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DrCarDrive.DBService
{
    class ExpensesDescriptionService
    {
        private DrcardriveDB db;

        public ExpensesDescriptionService()
        {
            db = new DrcardriveDB();
        }

        public int save(tbl_expense_desc desc)
        {
            DateTime currentDateTime = DateTime.Now;

            desc.INSERT_DATE = currentDateTime;
            desc.UPDATE_DATE = currentDateTime;

            if (desc != null)
            {
                db.ExpenseDesc.Add(desc);
            }
            return db.SaveChanges();
        }

        internal int update(tbl_expense_desc desc)
        {
            DateTime currentDateTime = DateTime.Now;
            desc.UPDATE_DATE = currentDateTime;

            if (desc != null)
            {
                var original = db.ExpenseDesc.SingleOrDefault(d => d.ID.Equals(desc.ID));
                db.Entry(original).CurrentValues.SetValues(desc);
            }
            return db.SaveChanges();
        }

        public List<tbl_expense_desc> viewAll()
        {
            return db.ExpenseDesc.OrderBy(e=>e.DESCRIPTION).ToList();
        }


        internal int remove(tbl_expense_desc desc)
        {
            if (desc != null)
            {
                tbl_expense_desc original = (tbl_expense_desc)db.ExpenseDesc.SingleOrDefault(d => d.ID.Equals(desc.ID));
                db.ExpenseDesc.Remove(original);
            }
            return db.SaveChanges();
        }

        public bool inDatabase(tbl_expense_desc desc)
        {
            return db.ExpenseDesc.Any(e => e.DESCRIPTION == desc.DESCRIPTION);
        }
    }
}
